package main

//type ListNode struct {
//	Val  int
//	Next *ListNode
//}

func swapPairs(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}
	dummy := &ListNode{}
	current := dummy
	xian := &ListNode{}
	hou := &ListNode{}
	hou2 := &ListNode{}
	current.Next = head
	for current != nil && current.Next != nil && current.Next.Next != nil {
		hou2 = current.Next.Next.Next
		//if hou2 == nil {
		//	hou2 = &ListNode{}
		//}
		xian = current.Next
		hou = current.Next.Next
		current.Next = hou
		current.Next.Next = xian
		current.Next.Next.Next = hou2
		current = current.Next.Next
	}

	return dummy.Next
}

//func printList2(head *ListNode) {
//	for head != nil {
//		fmt.Print(head.Val, " ")
//		head = head.Next
//	}
//	fmt.Println()
//}

func main() {

	l1 := &ListNode{}
	l2 := &ListNode{Val: 1}
	l3 := &ListNode{Val: 1, Next: &ListNode{Val: 2, Next: &ListNode{Val: 3, Next: &ListNode{Val: 4}}}}

	printList2(swapPairs(l1))
	printList2(swapPairs(l2)) // 输出: 空
	printList2(swapPairs(l3)) // 输出: 空

}
